from django.contrib import admin
from .models import Article, Notice, Category, Carousel, Comment, FriendLinks
from import_export.admin import ImportExportModelAdmin
from import_export import resources
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.shortcuts import reverse


# Register your models here
class ArticleResource(resources.ModelResource):
    class Meta:
        model = Article


class CategoryResource(resources.ModelResource):
    class Meta:
        model = Category


class CarouselResource(resources.ModelResource):
    class Meta:
        model = Carousel


class CommentResource(resources.ModelResource):
    class Meta:
        model = Comment


class FriendLinksResource(resources.ModelResource):
    class Meta:
        model = FriendLinks


@admin.register(Article)
class ArticleAdmin(ImportExportModelAdmin):
    resource_class = ArticleResource
    date_hierarchy = 'update_time'

    list_per_page = 10

    list_display = ['title', 'update_time', 'delete', 'username', 'category', 'hide', 'md']
    list_editable = ['delete', 'hide', 'category']
    list_filter = ['author_detail', 'top', 'category']
    search_fields = ('title',)

    ordering = ('-update_time',)

    def change_view(self, request, object_id, form_url='', extra_context=None):
        return HttpResponseRedirect(reverse('article-detail-view', kwargs={'article_id': object_id}))

    # 取消置顶
    def cancel_top(self, request, queryset):
        is_top = queryset.update(top=False)
        message_bit = ""
        if is_top == 1:
            message_bit = "取消推荐成功"

        self.message_user(request, message_bit)

    # 取消置顶
    def set_top(self, request, queryset):
        is_top = queryset.update(top=True)
        message_bit = ""
        if is_top == 1:
            message_bit = "推荐成功"

        self.message_user(request, message_bit)

    cancel_top.short_description = "取消推荐"
    set_top.short_description = "推荐"
    set_top.type = 'primary'
    cancel_top.type = 'danger'
    actions = [cancel_top, set_top]


@admin.register(Category)
class CategoryAdmin(ImportExportModelAdmin):
    resource_class = CarouselResource
    list_display = ['id', 'assortment', 'en_assortment', 'is_nav']
    list_editable = ['is_nav']


@admin.register(Notice)
class NoticeAdmin(admin.ModelAdmin):
    def changelist_view(self, request, extra_context=None):
        notice = self.model.objects.all().first()
        context = {
            'id': notice.id,
            'text': notice.text
        } if notice else {'id': '', 'text': ''}
        return render(request, template_name='admin/notice.html', context=context)


@admin.register(Carousel)
class CarouselModelAdmin(ImportExportModelAdmin):
    resource_class = CarouselResource
    list_display = ['href', 'img']


@admin.register(Comment)
class CommentModelAdmin(ImportExportModelAdmin):
    resource_class = CommentResource
    list_display = ['article_title', 'content', 'farther_comment_id']


@admin.register(FriendLinks)
class CarouselModelAdmin(ImportExportModelAdmin):
    resource_class = FriendLinksResource
    list_display = ['name', 'url']
